using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System.Collections.Generic;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_LevelMapQuestListPanel : UIBaseNode
    {
        #region Template Generate,don't modify

        protected class UIB_UIN_LevelMapQuestListPanel
        {
            #region ObjectBinding Generate

            public GameObject questItem { protected set; get; }
            public ExToggleGroup toggleGroup { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<GameObject>("questItem", out var __tbv0);
                questItem = __tbv0;
                __binding.TryGetVariableValue<ExToggleGroup>("toggleGroup", out var __tbv1);
                toggleGroup = __tbv1;
            }


            #endregion ObjectBinding Generate
        }

        #endregion Template Generate,don't modify

        #region fields

        private List<UIN_MapQuestItem> _items;

        #endregion fields

        #region properties

        protected UIB_UIN_LevelMapQuestListPanel ui { get; set; }

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_LevelMapQuestListPanel();
            ui.InitBinding(csObjBind);
        }

        protected override void OnInit()
        {
            _items = new List<UIN_MapQuestItem>
            {
                InitChildNode<UIN_MapQuestItem>(ui.questItem)
            };
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        protected override void OnDispose()
        {
            _items.Clear();
            _items = null;
        }

        #region methonds

        public void Refresh(int entityId, List<int> quests)
        {
            for (var i = 0; i < quests.Count; i++)
            {
                if (i == _items.Count)
                {
                    AddQuestItem();
                }

                _items[i].Show();
                _items[i].Refresh(entityId, quests[i]);
            }

            for (var i = quests.Count; i < _items.Count; i++)
            {
                _items[i].Hide();
            }

            ui.toggleGroup.SetAllTogglesOff(false);
        }

        private void AddQuestItem()
        {
            var go = Object.Instantiate(ui.questItem, ui.questItem.transform.parent);
            var item = InitChildNode<UIN_MapQuestItem>(go);
            ui.toggleGroup.RegisterToggle(item.toggle);
            _items.Add(item);
        }

        #endregion methonds
    }
}